<template>
  <div class="login">
    <el-form ref="form" :model="form" :rules="rules">
      <el-form-item>
        <img src="@/assets/common/login-logo.png" alt="">
      </el-form-item>
      <el-form-item prop="mobile">
        <el-input
          v-model="form.mobile"
          prefix-icon="el-icon-user-solid"
          @keyup.enter.native="submit"
        />
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          v-model="form.password"
          prefix-icon="el-icon-lock"
          show-password
          @keyup.enter.native="submit"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          class="loginBtn"
          type="primary"
          @click="submit"
        >登录</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
// import { sysLogin } from '@/api/user'
export default {
  data() {
    return {
      form: {
        mobile: '13800000002',
        password: '123456'
      },
      rules: {
        mobile: [
          { required: true, message: '必须填写手机号', trigger: 'change' },
          {
            // 自定义表单验证
            validator: (rules, value, callback) => {
              const reg =
                /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/
              if (reg.test(value)) {
                callback()
              } else {
                callback(new Error('请输入正确的手机号'))
              }
            }
          }
        ],
        password: [
          { required: true, message: '必须填写密码', trigger: 'change' },
          {
            min: 6,
            max: 12,
            message: '密码的长度必须为6-12位',
            trigger: 'change'
          }
        ]
      }
    }
  },
  methods: {
    submit() {
      // 全局表单验证
      this.$refs.form.validate(async(resluts) => {
        // const res = await sysLogin(this.form)
        // // 保存token
        // this.$store.commit('user/setToken', res.data)

        await this.$store.dispatch('user/toLogin', this.form)
        this.$message.success('登录成功')
        this.$router.push('/dashboard')
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.login {
  width: 100%;
  height: 100%;
  background: url('~@/assets/common/login.jpg') no-repeat;
  background-size: cover;
  display: flex;
  justify-content: center;
  align-items: center;
  .loginBtn {
    width: 100%;
    height: 50px;
  }
  ::v-deep .el-input__inner {
    height: 50px;
  }
}
</style>
